package sys_api

import (
	"fmt"
	"time"

	"github.com/gogf/gf/net/ghttp"
	cpu2 "github.com/shirou/gopsutil/cpu"
)

type userListReq struct {
	Email    string `p:"email"`
	Tel      string `p:"tel"`
	Status   int    `p:"status"`
	Name     string `p:"name"`
	Password string `p:"password"`
	Current  int    `p:"current"`
	PageSize int    `p:"pageSize"`
	ID       int    `p:"id"`
}

type userListRes struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

type Info struct {
	SysInfo SysInfo   `json:"sysInfo"`
	CpuTime []CpuTime `json:"cpuTime"`
}

type SysInfo struct {
	Cpu struct {
		Cpu1       int      `json:"cpu1"`
		Cpu2       int      `json:"cpu2"`
		CpuPercent float64  `json:"cpuPercent"`
		CPU        int32    `json:"cpu"`
		VendorID   string   `json:"vendorId"`
		Family     string   `json:"family"`
		Model      string   `json:"model"`
		Stepping   int32    `json:"stepping"`
		PhysicalID string   `json:"physicalId"`
		CoreID     string   `json:"coreId"`
		Cores      int32    `json:"cores"`
		ModelName  string   `json:"modelName"`
		Mhz        float64  `json:"mhz"`
		CacheSize  int32    `json:"cacheSize"`
		Flags      []string `json:"flags"`
		Microcode  string   `json:"microcode"`
	} `json:"cpu"`
}

type CpuTime struct {
	CPU       string  `json:"cpu"`
	User      float64 `json:"user"`
	System    float64 `json:"system"`
	Idle      float64 `json:"idle"`
	Nice      float64 `json:"nice"`
	Iowait    float64 `json:"iowait"`
	Irq       float64 `json:"irq"`
	Softirq   float64 `json:"softirq"`
	Steal     float64 `json:"steal"`
	Guest     float64 `json:"guest"`
	GuestNice float64 `json:"guestNice"`
	Percent   float64 `json:"percent"`
}

func (a *sysApi) ProcessList(r *ghttp.Request) {
	var req *userListReq
	if err := r.Parse(&req); err != nil {
		r.Response.WriteJsonExit(userListRes{
			Code: 0,
			Msg:  err.Error(),
		})
	}
	var info Info
	var sysInfo SysInfo

	sysInfo.Cpu.Cpu1, _ = cpu2.Counts(true)
	sysInfo.Cpu.Cpu2, _ = cpu2.Counts(false)

	cpuPercent, _ := cpu2.Percent(time.Duration(200)*time.Millisecond, false)
	sysInfo.Cpu.CpuPercent = cpuPercent[0]

	cpuInfo, _ := cpu2.Info()
	fmt.Println(cpuInfo)
	sysInfo.Cpu.CPU = cpuInfo[0].CPU
	sysInfo.Cpu.VendorID = cpuInfo[0].VendorID
	sysInfo.Cpu.Family = cpuInfo[0].Family
	sysInfo.Cpu.Model = cpuInfo[0].Model
	sysInfo.Cpu.Stepping = cpuInfo[0].Stepping
	sysInfo.Cpu.PhysicalID = cpuInfo[0].PhysicalID
	sysInfo.Cpu.CoreID = cpuInfo[0].CoreID
	sysInfo.Cpu.Cores = cpuInfo[0].Cores
	sysInfo.Cpu.ModelName = cpuInfo[0].ModelName
	sysInfo.Cpu.Mhz = cpuInfo[0].Mhz
	sysInfo.Cpu.CacheSize = cpuInfo[0].CacheSize
	sysInfo.Cpu.Flags = cpuInfo[0].Flags
	sysInfo.Cpu.Microcode = cpuInfo[0].Microcode

	//info.CpuInfo = cpuInfos

	var cpuTimes []CpuTime
	cpuPercents, _ := cpu2.Percent(time.Duration(200)*time.Millisecond, true)
	cpuPercents = append(cpuPercents, cpuPercent...)
	cpuTime1, _ := cpu2.Times(true)
	cpuTime2, _ := cpu2.Times(false)
	cpuTime1 = append(cpuTime1, cpuTime2...)
	for key, item := range cpuPercents {
		var cpuTime CpuTime
		cpuTime.CPU = cpuTime1[key].CPU
		cpuTime.User = cpuTime1[key].User
		cpuTime.System = cpuTime1[key].System
		cpuTime.Idle = cpuTime1[key].Idle
		cpuTime.Nice = cpuTime1[key].Nice
		cpuTime.Iowait = cpuTime1[key].Iowait
		cpuTime.Irq = cpuTime1[key].Irq
		cpuTime.Softirq = cpuTime1[key].Softirq
		cpuTime.Steal = cpuTime1[key].Steal
		cpuTime.Guest = cpuTime1[key].Guest
		cpuTime.GuestNice = cpuTime1[key].GuestNice
		cpuTime.Percent = item
		cpuTimes = append(cpuTimes, cpuTime)
	}
	info.SysInfo = sysInfo
	info.CpuTime = cpuTimes
	/*cpuTime2, _ := cpu2.Times(false)

	cpuTime := append(cpuTime1, cpuTime2...)
	for _, item := range cpuTime {
		cpuInfos = append(cpuInfos, item)
	}*/
	fmt.Println(info)
	r.Response.WriteJsonExit(userListRes{
		Code: 1,
		Data: info,
	})
	/*p, _ :=process.Processes()

	for _,item :=range p{
		//p1,_ :=process.NewProcess(item)
		fmt.Println(item.Name())
		fmt.Println(item.Background())
		fmt.Println(item.CPUAffinity())
		fmt.Println(item.CPUPercent())
		fmt.Println(item.Children())
		fmt.Println(item.Cmdline())
		fmt.Println(item.CmdlineSlice())
		fmt.Println(item.Connections())
		fmt.Println(item.CreateTime())
		fmt.Println(item.Cwd())
		fmt.Println(item.Exe())
		fmt.Println(item.Exe())
		fmt.Println(item.Foreground())
		fmt.Println(item.Gids())
		fmt.Println(item.Groups())
		fmt.Println(item.IOCounters())
		fmt.Println(item.IOnice())
		fmt.Println(item.IsRunning())
		fmt.Println(item.MemoryInfo())
		fmt.Println(item.MemoryInfoEx())
		fmt.Println(item.MemoryPercent())
		fmt.Println(item.Nice())
		fmt.Println(item.NumCtxSwitches ())
		fmt.Println(item.NumFDs ())
		fmt.Println(item.NumFDs ())
		fmt.Println(item.NumThreads ())
		fmt.Println(item.OpenFiles ())
		fmt.Println(item.PageFaults ())
		fmt.Println(item.Parent ())
		fmt.Println(item.Ppid ())
		fmt.Println(item.Rlimit ())
		fmt.Println(item.Status ())
		fmt.Println(item.String ())
		fmt.Println(item.Tgid ())
		fmt.Println(item.Threads ())
		fmt.Println(item.Times ())
		fmt.Println(item.Uids ())
		fmt.Println(item.Username ())
	}*/
	r.Response.WriteJsonExit(userListRes{
		Code: 1,
	})
}
